package com.uwlaser.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.uwlaser.domain.CraftOperation;
import com.uwlaser.domain.CraftPathDetail;
import com.uwlaser.service.mapper.CraftOperationMapper;
import com.uwlaser.service.mapper.CraftPathDetailMapper;
import lombok.AllArgsConstructor;
import com.uwlaser.base.PageInfo;
import com.uwlaser.base.QueryHelpMybatisPlus;
import com.uwlaser.base.impl.CommonServiceImpl;
import com.uwlaser.utils.ConvertUtil;
import com.uwlaser.utils.PageUtil;
import com.uwlaser.domain.CraftOperationProduct;
import com.uwlaser.service.CraftOperationProductService;
import com.uwlaser.service.dto.CraftOperationProductDto;
import com.uwlaser.service.dto.CraftOperationProductQueryParam;
import com.uwlaser.service.mapper.CraftOperationProductMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Pageable;
import java.util.*;
import java.util.stream.Collectors;

/**
* @author zk-Qin
* @date 2022-05-11
*/
@Service
@AllArgsConstructor
// @CacheConfig(cacheNames = CraftOperationProductService.CACHE_KEY)
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class CraftOperationProductServiceImpl extends CommonServiceImpl<CraftOperationProductMapper, CraftOperationProduct> implements CraftOperationProductService {

    // private final RedisUtils redisUtils;
    private final CraftOperationProductMapper craftOperationProductMapper;

    private final CraftOperationMapper craftOperationMapper;

    private final CraftPathDetailMapper craftPathDetailMapper;

    @Override
    public PageInfo<CraftOperationProductDto> queryAll(CraftOperationProductQueryParam query, Pageable pageable) {
        IPage<CraftOperationProduct> queryPage = PageUtil.toMybatisPage(pageable);
        IPage<CraftOperationProduct> page = craftOperationProductMapper.selectPage(queryPage, QueryHelpMybatisPlus.getPredicate(query));
        return ConvertUtil.convertPage(page, CraftOperationProductDto.class);
    }

    @Override
    public List<CraftOperationProductDto> queryAll(CraftOperationProductQueryParam query){
        return ConvertUtil.convertList(craftOperationProductMapper.selectList(QueryHelpMybatisPlus.getPredicate(query)), CraftOperationProductDto.class);
    }

    @Override
    public CraftOperationProduct getById(Integer id) {
        return craftOperationProductMapper.selectById(id);
    }

    @Override
    // @Cacheable(key = "'id:' + #p0")
    public CraftOperationProductDto findById(Integer id) {
        return ConvertUtil.convert(getById(id), CraftOperationProductDto.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insert(CraftOperationProductDto resources) {
        CraftOperationProduct entity = ConvertUtil.convert(resources, CraftOperationProduct.class);
        return craftOperationProductMapper.insert(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateById(CraftOperationProductDto resources){
        CraftOperationProduct entity = ConvertUtil.convert(resources, CraftOperationProduct.class);
        int ret = craftOperationProductMapper.updateById(entity);
        // delCaches(resources.id);
        return ret;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeByIds(Set<Integer> ids){
        // delCaches(ids);
        return craftOperationProductMapper.deleteBatchIds(ids);
    }

    /**
     * 查询产品相关的工序
     * @param query
     * @return
     */
    @Override
    public List<CraftOperation> getByProduct(CraftOperationProductQueryParam query) {

        List<CraftOperationProduct> operationProducts = craftOperationProductMapper.selectList(QueryHelpMybatisPlus.getPredicate(query));

        if(operationProducts == null || operationProducts.size() == 0){
            return new ArrayList<>();
        }

        List<String> ops = operationProducts.stream().map(CraftOperationProduct::getOperationCode).collect(Collectors.toList());

        return craftOperationMapper.selectList(new QueryWrapper<CraftOperation>().in("operation_code", ops));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeById(Integer id){
        Set<Integer> set = new HashSet<>(1);
        set.add(id);
        return this.removeByIds(set);
    }

    /*
    private void delCaches(Integer id) {
        redisUtils.delByKey(CACHE_KEY + "::id:", id);
    }

    private void delCaches(Set<Integer> ids) {
        for (Integer id: ids) {
            delCaches(id);
        }
    }*/

    /*
    @Override
    public void download(List<CraftOperationProductDto> all, HttpServletResponse response) throws IOException {
      List<Map<String, Object>> list = new ArrayList<>();
      for (CraftOperationProductDto craftOperationProduct : all) {
        Map<String,Object> map = new LinkedHashMap<>();
              map.put("产品编码", craftOperationProduct.getProductCode());
              map.put("工序编号", craftOperationProduct.getOperationCode());
        list.add(map);
      }
      FileUtil.downloadExcel(list, response);
    }*/
}
